package application;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ControlPanelController implements ActionListener, ItemListener
{
	// Components
	protected CanvasView canvas;
	
	/**
	 * Constructor that creates Control Panel Controller that is associated with
	 * the specified Canvas
	 * 
	 * @param c associated DrawingCanvas
	 */
	ControlPanelController(CanvasView c) 
	{
		if( c != null )
			canvas = c;
	    else
	    	throw new IllegalArgumentException();
	}
	
	/******Event Handlers********************************/
	
	/* (non-Javadoc)
	 * 
	 * Fields actions performed by the Clear Canvas button on a ControlPanelView
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e){ canvas.clearCanvas(); }

	/* (non-Javadoc)
	 * 
	 * Fields actions performed by the color combo box on a ControlPanelView
	 * 
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	public void itemStateChanged(ItemEvent e)
	{
		Color temp;
		if (e.getStateChange() == ItemEvent.SELECTED)
		{
			temp = itemToColor(e.getItem());
			canvas.setpenColor(temp);
			
			if(canvas.getCurrentToolName() == "Object")
				canvas.changeObjectColor(temp);
		}
	}

	/**
	 * Used to determine which color to select. Maps a string constant to a new
	 * color. If the supplied argument is null or unrecognized, the pen color
	 * will remain the same. 
	 * 
	 * @param item Selected item from a ControlPanelView's combo box
	 * @return Associated color
	 */
	protected Color itemToColor(Object item)
	{
		if( item == null )
			return canvas.getpenColor();
	    
	    if("Black".equals(item))
	    	return Color.black;
	    else if("Blue".equals(item))
	    	return Color.blue;
	    else if("Red".equals(item))
	    	return Color.red;
	    else if("Yellow".equals(item))
	    	return Color.yellow;
	    else
	    	return canvas.getpenColor();
	  }// end protected Color itemToColor( Object )
}
